cmake_minimum_required(VERSION 2.8.8)

project(OSGi CXX C)

if(WIN32)
  enable_language(RC)
  set(CMAKE_RC_COMPILER_INIT windres)
  set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff -i <SOURCE> -o <OBJECT>")
endif()

# Versions.
set(OSGi_VERSION_MAJOR 0)
set(OSGi_VERSION_MINOR 9)
set(OSGi_VERSION_PATCH 43)
set(OSGi_VERSION "${OSGi_VERSION_MAJOR}.${OSGi_VERSION_MINOR}.${OSGi_VERSION_PATCH}")
set(OSGi_VERSION_BUILD 0)   # The build version is currently unused.

set(VALID_VERSION_STRING "0.5.12")

add_definitions(-DOSGI_VERSION="${OSGi_VERSION}" -DVALID_VERSION="${VALID_VERSION_STRING}")

# Build configuration.
include_directories(${OSGi_SOURCE_DIR}/OSGi)
# Debug.
if(NOT CMAKE_BUILD_TYPE OR "${CMAKE_BUILD_TYPE}" MATCHES Release)
    add_definitions(-DRELEASE -DPOSTFIX="")
elseif(${CMAKE_BUILD_TYPE} MATCHES Debug)
    add_definitions(-DDEBUG   -DPOSTFIX="d")
else()
    message(CRITICAL_ERROR " Unknown build type : ${CMAKE_BUILD_TYPE}")
endif()
set(CMAKE_DEBUG_POSTFIX "d")
# GCC.
if(${CMAKE_COMPILER_IS_GNUCXX})
    set(CMAKE_CXX_FLAGS_DEBUG   "-O0 -ggdb")
    set(CMAKE_CXX_FLAGS_RELEASE "-O3 -fexpensive-optimizations -fno-strength-reduce -fomit-frame-pointer")

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fvisibility=default -DPIC -pipe")
    if(MINGW)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMINGW -D_NO_W32_PSEUDO_MODIFIERS -U__STRICT_ANSI__")
    elseif(UNIX)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLINUX -fPIC -Werror")
        set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   -DLINUX -fPIC -Werror")
    endif()
endif()

# Install configuration.
if(WIN32 AND NOT CYGWIN AND NOT MINGW)
    set(DEF_INSTALL_CMAKE_DIR CMake)
else()
    set(DEF_INSTALL_CMAKE_DIR lib/CMake/OSGi)
endif()
# Offers the user the choice of overriding the installation directories
if(WIN32)
set(INSTALL_LIB_DIR     bin                      CACHE PATH "Installation directory for libraries")
else()
set(INSTALL_LIB_DIR     lib                      CACHE PATH "Installation directory for libraries")
endif()
set(INSTALL_BIN_DIR     bin                      CACHE PATH "Installation directory for executables")
set(INSTALL_INCLUDE_DIR include                  CACHE PATH "Installation directory for header files")
set(INSTALL_CMAKE_DIR   ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files")
# Computes absolute paths.
foreach(p LIB BIN INCLUDE CMAKE)
    set(var INSTALL_${p}_DIR)
    if(NOT IS_ABSOLUTE "${${var}}")
        set(ABSOLUTE_${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
    else()
        set(ABSOLUTE_${var} "${${var}}")
    endif()
endforeach()

# Components.
add_subdirectory(OSGi)
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(bundles)
add_subdirectory(html)

# Install.
export(TARGETS OSGi++ osgi-packager FILE ${OSGi_BINARY_DIR}/OSGiTargets.cmake)
export(PACKAGE OSGi)

file(RELATIVE_PATH REL_INCLUDE_DIR "${ABSOLUTE_INSTALL_CMAKE_DIR}" "${ABSOLUTE_INSTALL_INCLUDE_DIR}")

set(CONF_INCLUDE_DIR "${OSGi_SOURCE_DIR}" "${OSGi_BINARY_DIR}")
configure_file("${OSGi_SOURCE_DIR}/CMake/OSGiConfig.cmake.in"           "${OSGi_BINARY_DIR}/OSGiConfig.cmake" @ONLY)
configure_file("${OSGi_SOURCE_DIR}/CMake/OSGiUse.cmake.in"              "${OSGi_BINARY_DIR}/OSGiUse.cmake"    @ONLY)

set(CONF_INCLUDE_DIR "\${OSGi_CMAKE_DIR}/${REL_INCLUDE_DIR}")
configure_file("${OSGi_SOURCE_DIR}/CMake/OSGiConfig.cmake.in"           "${OSGi_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/OSGiConfig.cmake" @ONLY)
configure_file("${OSGi_SOURCE_DIR}/CMake/OSGiUse.cmake.in"              "${OSGi_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/OSGiUse.cmake"    @ONLY)
configure_file("${OSGi_SOURCE_DIR}/CMake/OSGiConfigVersion.cmake.in"    "${OSGi_BINARY_DIR}/OSGiConfigVersion.cmake"                  @ONLY)

# Installs OSGiConfig.cmake and OSGiConfigVersion.cmake .
install(FILES
    "${OSGi_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/OSGiConfig.cmake"
    "${OSGi_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/OSGiUse.cmake"
    "${OSGi_BINARY_DIR}/OSGiConfigVersion.cmake"
    DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev
)

# Installs the export set for use with the install-tree.
install(EXPORT OSGiTargets DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev)

# Packaging.
include(InstallRequiredSystemLibraries)
set(CPAK_RESOURCE_FILE_LICENSE  "${OSGi_SOURCE_DIR}/COPYING")
set(CPACK_PACKAGE_VERSION_MAJOR "${OSGi_VERSION}")
set(CPACK_PACKAGE_VERSION_MAJOR "${OSGi_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${OSGi_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${OSGi_VERSION_PATCH}")
set(CPACK_PACKAGE_ICON          "${OSGi_SOURCE_DIR}/src/packager/packager.ico")
set(CPACK_GENERATOR             "TBZ2")
include(CPack)

